<template>
  <div class="content">
    <div class="sign-up-container">
      <div class="title">
        <nuxt-link to="/forgetPassword" active-class="active">
          找回密码
        </nuxt-link>
      </div>
      <el-form ref="userForm" :model="user">
        <el-form-item
          class="input-prepend restyle"
          prop="mobile"
          :rules="[
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { validator: checkPhone, trigger: 'blur' },
          ]"
        >
          <div>
            <el-input
              type="text"
              placeholder="手机号"
              v-model="user.mobile"
              prefix-icon="el-icon-mobile"
            />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend restyle no-radius"
          prop="code"
          :rules="[
            { required: true, message: '请输入验证码', trigger: 'blur' },
          ]"
        >
          <div
            style="width: 100%; display: block; float: left; position: relative"
          >
            <el-input
              type="text"
              placeholder="验证码"
              v-model="user.code"
              prefix-icon="el-icon-message"
            />
          </div>
          <div class="codetest">
            <el-button type="text" @click="getCodeFun()" :disabled="!sending">{{
              codeTest
            }}</el-button>
            <!-- <a
              href="javascript:"
              type="button"
              @click="getCodeFun()"
              :value="codeTest"
              style="border: none"
              >{{ codeTest }}</a
            > -->
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[
            { required: true, message: '请输入新密码', trigger: 'blur' },
          ]"
        >
          <div>
            <el-input
              type="password"
              placeholder="新密码"
              v-model="user.password"
              prefix-icon="el-icon-lock"
            />
          </div>
        </el-form-item>

        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="重置密码"
            @click="resetPassword()"
          />
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import "~/assets/css/sign.less";
import "~/assets/css/iconfont.css";
import loginApi from "@/api/login";
import registerApi from "@/api/register";

export default {
  layout: "sign",

  data() {
    return {
      user: {
        mobile: "",
        code: "",
        password: "",
      },
      sending: true, //是否发送验证码
      second: 60, //倒计时间
      codeTest: "获取验证码",
      isMobileExist: false,
      loginInfo: {},
    };
  },

  methods: {
    resetPassword() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          loginApi.forgetPassword(this.user).then((response) => {
            if (response.data.success) {
              this.$message.success('重置密码成功');
              this.$router.push({ path: "/login" });
            }
          });
        }
      });
    },

    timeDown() {
      let result = setInterval(() => {
        --this.second;
        this.codeTest = this.second;
        if (this.second < 1) {
          clearInterval(result);
          this.sending = true;
          //this.disabled = false;
          this.second = 60;
          this.codeTest = "获取验证码";
        }
      }, 1000);
    },

    getCodeFun() {
      //sending = false
      //his.sending原为true,请求成功，!this.sending == true，主要是防止有人把disabled属性去掉，多次点击；
      if (!this.sending) return;

      //debugger
      // prop 换成你想监听的prop字段
      this.$refs.userForm.validateField("mobile", (errMsg) => {
        if (errMsg == "") {
          registerApi.sendCode(this.user.mobile).then((res) => {
            this.sending = false;
            this.timeDown();
          });
        }
      });
    },

    checkPhone(rule, value, callback) {
      //debugger
      if (!/^1[345789]\d{9}$/.test(value)) {
        return callback(new Error("手机号码格式不正确"));
      }
      return callback();
    },
  },
};
</script>
<style>
.el-form-item__error {
  z-index: 9999999;
}
</style>
